package LeetCode;

import java.util.Iterator;

public class LC_284_PeekingIterator {

    public static void main(String[] args) {

    }

    class PeekingIterator implements Iterator<Integer> {

        Iterator<Integer> iterator;
        int value;
        boolean isValid;

        public PeekingIterator(Iterator<Integer> iterator) {
            this.iterator = iterator;
            value = -1;
            isValid = false;
        }

        public Integer peek() {
            if (isValid)
                return value;
            int val = iterator.next();
            value = val;
            isValid = true;
            return val;
        }

        @Override
        public Integer next() {
            if (isValid) {
                isValid = false;
                return value;
            }
            return iterator.next();
        }

        @Override
        public boolean hasNext() {
            return isValid || iterator.hasNext();
        }
    }

}